Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Switch

Switch in Java

Switch

The switch statement is a multiway branching statement that is used to select one of many blocks of code to be executed. The switch statement is a replacement for the if-else-if ladder. The switch statement works by comparing the value of an expression to a list of values. If the value of the expression matches one of the values in the list, then the corresponding block of code is executed. If the value of the expression does not match any of the values in the list, then the default block of code is executed. The syntax for the switch statement is as follows:
Switch-syntax switch (expression) { case value1: // block of code break; case value2: // block of code break; ... default: // block of code }
The expression in the switch statement can be of any type that can be compared to a value, such as an integer, a string, or a character. The values in the case statements can be literal values, variables, or expressions. The break statement is used to terminate the switch statement. If the break statement is not used, the switch statement will continue to execute the statements in the next case statement, even if the value of the expression does not match. The default case is executed if the value of the expression does not match any of the values in the case statements. The default case is optional. Here is an example of a switch statement:
Switch example
public class Main{ public static void main(String args[]){ int dayOfWeek = 2; switch (dayOfWeek) { case 1: System.out.println("Today is Monday"); break; case 2: System.out.println("Today is Tuesday"); break; case 3: System.out.println("Today is Wednesday"); break; case 4: System.out.println("Today is Thursday"); break; case 5: System.out.println("Today is Friday"); break; case 6: System.out.println("Today is Saturday"); break; case 7: System.out.println("Today is Sunday"); break; default: System.out.println("Invalid day of the week"); } } }

Output

Today is Tuesday
In this example, the switch statement will print the day of the week corresponding to the value of the variable dayOfWeek. If the value of dayOfWeek is not a valid day of the week, then the default message "Invalid day of the week" will be printed. The switch statement is a powerful tool that can be used to make code more concise and readable. However, it is important to use it carefully, as it can make code more difficult to understand if it is not used correctly. Here are some additional things to keep in mind about the switch statement: The break statement is optional, but it is generally a good idea to use it to terminate the switch statement as soon as a matching case is found. The default case is optional, but it is a good idea to include it in case the value of the expression does not match any of the case statements. The switch statement can be nested, but it is generally a good idea to avoid nesting switch statements too deeply.

  📌TAGS

★switch ★switch statement ★control statement ★control in java ★switch in java

Tutorials